var app = getApp()

//拼团列表
function groupbuyList(callback) {
  let data = {
    "userId": app.globalData.userId
  }
  app.func.Get(true, app.api.groupbuy_list_book, data ,function (res) {
    typeof callback == "function" && callback(res)
  })
};

//拼团商品详情
function groupbuyGoodsDetail(id, callback) {
  let data = { 
    "groupBuyId": id,
    "userId": app.globalData.userId
     }
  app.func.Get(true, app.api.groupbuy_list_bookDetail, data, function (res) {
    typeof callback == "function" && callback(res);
  });
};

/**
 * 订单信息
 */
function orderDetail(groupBuyId, success,fail) {
  let param = new Object();
  param['userId'] = app.globalData.userId;
  param['groupBuyId'] = groupBuyId;
  app.func.Get(false, app.api.groupbuy_countersign, param, function (data){
    typeof success == "function" && success(data);
  },
    function (error) {
      console.log('提交订单失败' + error);
      typeof fail == "function" && fail(error);
    });
}
/**
 * 提交订单
 */
function submitOrder(groupBuyId,addressId, success,fail) {
  let param = new Object();
  param['userId'] = app.globalData.userId;
  param['groupBuyId'] = groupBuyId;
  param['addressId'] = addressId;
  app.func.Post(false, app.api.groupbuy_commit, param, function (data) {
    typeof success == "function" && success(data);
  },
    function (error) {
      typeof fail == "function" && fail(error.data);
    });
}

//拼团详情
function groupbuyDetail(id, groupBuyUserId, callback) {
  let data = {
    'groupBuyId': id,
    'userId': app.globalData.userId,
    'groupBuyUserId': groupBuyUserId,
  }
  app.func.Post(false, app.api.groupbuy_groupDetail, data, function (res) {
    typeof callback == "function" && callback(res);
  });
};

//更多商品
function moreGoods(starPage, callback) {
  let data = {
    'userId': app.globalData.userId,
    'goodsType': 0,
    'startPage': starPage
  }
  app.func.Get(true, app.api.goods_list_more, data, function (res) {
    typeof callback == "function" && callback(res)
  })
};

//参加拼团
function groupbuyJoin(grouperId, id, groupBuyUserId, success,fail) {
  let data = {
    'grouperId': grouperId,
    'groupBuyId': id,
    "groupBuyUserId": groupBuyUserId,
    'userId': app.globalData.userId,
  }
  app.func.Post(false, app.api.join_groupbuy_book, data, function (res) {
    typeof success == "function" && success(res)
  }, function (error) {
    typeof fail == "function" && fail(error)
  });
};

/**
* 获取规则信息
*/
function getRule(type, successCall) {
  getApp().func.Get(false, app.api.get_setting, { "type": type },
    function (data) {
      successCall(data)
    })
};

module.exports = {
  groupbuyList: groupbuyList,
  groupbuyGoodsDetail: groupbuyGoodsDetail,
  groupbuyDetail: groupbuyDetail,
  moreGoods: moreGoods,
  groupbuyJoin: groupbuyJoin,
  submitOrder: submitOrder,
  orderDetail: orderDetail,
  getRule: getRule

};